package com.seocoo.worker.home;

import android.app.Application;
import android.content.pm.PackageInfo;

import com.seocoo.worker.base.entity.EVersion;
import com.seocoo.worker.base.http.BaseRsp;
import com.seocoo.worker.base.http.DWBaseRequest;
import com.sfh.lib.AppCacheManager;

/***
 * 版本检查
 */
public class ReqDwmVersion extends DWBaseRequest<BaseRsp<EVersion>> {

    //版本类型1-安卓用户端2-安卓工匠端3-安卓商家端
    String versionType = "2";

    public ReqDwmVersion() {
        super("/api/openapi/dwmVersion");
    }

    @Override
    public void cacheResponse(BaseRsp<EVersion> data) {
        super.cacheResponse(data);
        if (data.success()) {
            EVersion update = data.getData();

            int remoteVersion = update.version;
            //step_2 比对版本号
            final int localVersion = getLocationVersionCode(AppCacheManager.getApplication());

            // 服务器版本 <= 当前版本
            if (remoteVersion <= localVersion) {
                // 不需要更新
                update.show = false;

            } else {
                //需要更新
                update.show = true;
            }
        }
    }

    /**
     * 本地安装版本号text
     *
     * @return
     */
    public static int getLocationVersionCode(Application context) {

        try {
            PackageInfo packageInfo = context.getApplicationContext()
                    .getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionCode;
        } catch (Exception e) {
        }
        return -1;
    }
}
